﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;

namespace Elementum
{
    class Stage6 : Stage
    {
        float timeTillPuff = 0.0f;
        SmokePlumeParticleSystemForStage6 smokePlume;
        const float TimeBetweenSmokePlumePuffs = 0.5f;

        float GROUND_ALPHA = 0f;

        public Stage6(Game game) : base(game) { }

        public override void InitializeScenes()
        {
            base.InitializeScenes();

            for (int i = 1; i < 4; i++)
            {
                Animation a = new Animation("images/stages/stage3/city_night" + i);
                a.LayerDepth = 0.9f;
                _backGround.Add(a);
            }

            _backGroundPos = new Vector2(-435, -400);
            _scenceBound = new Rectangle(100, 0,
               1500,
              300);
            camera.Bound = _scenceBound;
            camera.CameraTransfer = 100;

            for (int i = 0; i < 3; i++)
            {
                Animation wallBack = new Animation("images/stages/stage6/stage6_" + i);
                //wallBack.LayerDepth = 0.9f;
                _wallBackGround.Add(wallBack);
            }
            _wallBackGroungPos = new Vector2(-370, -204);

            // basePos = player.Position;

            smokePlume = new SmokePlumeParticleSystemForStage6(this,2);

            Animation title = new Animation("images/title/stageTitle", new Rectangle(2, 100, 196, 50), new Point(196, 50));
            title.Scale = 1.2f;

            Transition tra;
            tra = new DefauldTransition(1, 0, 0, 60);
            tra.Register(this);
            tra = new TitleTransition(title, new Vector2(483, 100), 0, 1, 80, 220);
            tra.Register(this);


            GroundObject grount =
            StateFactory.CreateGround(this, 10, 226, 100, 600, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 260, 317, 400, 25, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 210, 392, 300, 50, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 520, 392, 200, 50, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 660, 317, 400, 25, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 755, 392, 150, 50, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 953, 392, 125, 50, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 1214, 392, 400, 50, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 902, 317, 84, 25, 0); grount.Alpha = GROUND_ALPHA; 
            StateFactory.CreateDoor("Stage6_Door_S1", this, 1139+120, 317, "Stage6_Door_1");
            StateFactory.CreateKey("Stage6_Key_S1", this, 109, 350, "Stage6_Door_1");
            StateFactory.CreatePortal("Stage6_Portal_1", this, 360, 253, "stage0", "Machine_Protal2");//
            MechanismGound mechanismGound =
            StateFactory.CreateMechanismGound(this, 390, 422, 52, 10, 0, 0, -50, 0, 20); 
            
            Switch gameSwitch =
            StateFactory.CreateSwitch(this, 510, 279); gameSwitch.AddTarget(mechanismGound);
            mechanismGound = 
            StateFactory.CreateMechanismGound(this, 650, 372, 52, 10, 0, 0, 50, 0, 20);gameSwitch.AddTarget(mechanismGound);
            
            gameSwitch =
            StateFactory.CreateSwitch(this, 743, 279);gameSwitch.AddTarget(mechanismGound);
            mechanismGound =
            StateFactory.CreateMechanismGound(this, 860, 422, 52, 10, 0, 0, -50, 0, 20);
            gameSwitch.AddTarget(mechanismGound);
            
            grount =
            StateFactory.CreateGround(this, 1370, 217, 400, 25, 0); grount.Alpha = GROUND_ALPHA; 
            gameSwitch =
            StateFactory.CreateSwitch(this, 919, 279);
            gameSwitch.AddTarget(mechanismGound);
            //mechanismGound =
            //StateFactory.CreateMechanismGound(this, 1095, 217, 150, 25, 0, 0, 300, 0, 1);
            //gameSwitch.AddTarget(mechanismGound);
            
            MechanismGound mechanismGound1 = new MechanismGound(this._world, this);
            mechanismGound1.Position = new Vector2(1095, 217);
            mechanismGound1.CurrentAnimation = new Animation("images/items/mechineWall", new Point(150, 25));
            mechanismGound1.Size = new Vector2(150, 25);
            mechanismGound1.Rotation = 0f;
            Actors.Add(mechanismGound1);

            ModifierContain first = new ModifierContain(mechanismGound1);
            ModifierContain next = new ModifierContain(mechanismGound1);
            //設定機關行為
            RotationModifier m2 = new RotationModifier(0f, MathHelper.PiOver2, 0, 80);
            RotationModifier m3 = new RotationModifier(MathHelper.PiOver2, 0, 20, 100);
            first.AddModifier(m2);
            next.AddModifier(m3);

            mechanismGound1.PositiveAction.Add(first);// = first;
            mechanismGound1.NegetiveAction.Add(next); // = next;

            gameSwitch.AddTarget(mechanismGound1);
            

            //MechanismGound mechanismGound1 =
            //StateFactory.CreateMechanismGound(this, 1095, 217, 150, 25, 0, -10, 0, 0, 10);//旋轉
            //RotationModifier rotationModifier = new RotationModifier(0f,3.14f,0,30);
            grount =
            StateFactory.CreateGround(this, 1288, 100, 50, 208, 0);grount.Alpha = GROUND_ALPHA;
            Floor floor =
            StateFactory.CreateFloor(this, 1346, 150, 40, 141, 0, 0, 110, 0, 30);
            SensorSwitch sensorSwitch =
            StateFactory.CreateSensorSwitch(this, 993, 352); sensorSwitch.AddTarget(floor);
            StateFactory.CreatePortal("Stage6_Portal_2", this, 1470, 154, "stage7", "Stage7_Portal_1");
            StateFactory.CreatekillBund(this, 1606, 429, 384, 64, 0);
            StateFactory.CreatekillBund(this, 1760, 192, 384, 64, 1.57f);

            Monster m = new Monster(this);
            m.Position = new Vector2(1150, 168);//1150, 168
            actors.Add(m);

            MusicManager.AddMusic("Music/doorlock", "doorlock", 0.001f, false);
            _bloom = _gameClass._BloomComponent;

            actors.Add(smokePlume);

            _song = MusicManager.LoadSong("Music/stage6");
            if (GameClass._musicString == "Music/stage6")
            { }
            else
            {
                GameClass._musicString = "Music/stage6";
                MediaPlayer.Play(_song);
            }
            MediaPlayer.IsRepeating = true;
           // MediaPlayer.Volume = 1f;

            _startProtal = new ProtalCommand("stage6", "Stage6_Portal_1");

        }

        public override void Restart()
        {
            StageManager.Save.StageStateTable.Remove(StageManager.CurrentStateID);
            StageManager.ProtalCommand = new ProtalCommand("stage6", "Stage6_Portal_1");
        }

        public override void OnDraw(SpriteBatch spriteBatch, int delta)
        {
            //_bloom.BeginDraw();
            //GraphicsDevice.Clear(Color.Black);

            //a.OnDraw(spriteBatch, new Vector2(0,0), camera.Transform);
            //_bloom.Draw();

            base.OnDraw(spriteBatch, delta);

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.Transform);
            // basePos = player.Position;

            foreach (VisibleObject b in actors)
                b.OnDraw(spriteBatch, camera.Transform);

            spriteBatch.End();

            //spriteBatch.Begin();
            //smokePlume.OnDraw(spriteBatch, camera.Transform);
            //   spriteBatch.DrawString(Font,   ""+camera.IsFocus+
            //(camera.Focus == player)+
            //(camera.Bound == _scenceBound), new Vector2(50, 50), Color.White);
            //spriteBatch.End();
            PlayerCollisionDetector con = player.CollisionListener;
            //spriteBatch.Begin();
            //spriteBatch.DrawString(Font, "Po" + con.buttonList.Count(), new Vector2(50, 50), Color.Black);
            //spriteBatch.DrawString(Font, "temp" + mechanismGound.tempCount + "count" + mechanismGound.PositiveAction.Count, new Vector2(50, 50), Color.Black);
            //spriteBatch.End();

        }

        public override void Update(int delta)
        {
            base.Update(delta);
            //camera.IsFocus = true;
            //camera.Focus = player;
            //camera.Bound = _scenceBound;
            //camera.Update(delta);
           // smokePlume.Update(delta);
            UpdateSmokePlume(0.01f);
            //camera.Update(delta);
            //a.Update(delta);
            //  MusicManager.FadeInMusic("first");
            //MusicManager.FadeOutMusic("first");
        }

        private void UpdateSmokePlume(float dt)
        {
            timeTillPuff -= dt;
            if (timeTillPuff < 0)
            {
                Vector2 where = Vector2.Zero;
                // add more particles at the bottom of the screen, halfway across.
                //this.Game.GraphicsDevice.Viewport;
                where.X = GameClass.RandomBetween(_scenceBound.Left, _scenceBound.Right);
                where.Y = GameClass.RandomBetween(_scenceBound.Top, _scenceBound.Bottom);
                smokePlume.AddParticles(where);

                // and then reset the timer.
                timeTillPuff = TimeBetweenSmokePlumePuffs;
            }
        }

        public override Stage Clone()
        {
            Stage6 stage = new Stage6(Game);
            stage.InitializeScenes();
            return stage;
        }
    }
}
